package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-24
 * Time：11:40
 */
@WebServlet("/remark")
public class RemarkServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //1.检查登陆状态
        HttpSession session = req.getSession(false);
        if (session == null) {
            System.out.println("session为null");
            resp.setStatus(302);
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            System.out.println("user为null");
            resp.setStatus(302);
            return;
        }
        String json = null;
        List<Remark> remarkList = new ArrayList<>();

        String blogId = req.getParameter("blogId");
        if (blogId==null) {
            System.out.println("传参错误！");
            return;
        }

        int bId = Integer.parseInt(blogId);

        RemarkDao remarkDao = new RemarkDao();
        remarkList = remarkDao.selectById(bId);
        json = objectMapper.writeValueAsString(remarkList);
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //1.检查登陆状态
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setStatus(302);
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.setStatus(302);
            return;
        }

        String content = req.getParameter("content");
        if (content == null) {
            System.out.println("传参错误！");
            return;
        }

        String blogId = req.getParameter("blogId");
        if (blogId == null) {;
            System.out.println("传参错误！");
            return;
        }
        int bId = Integer.parseInt(blogId);

        int userId = user.getUserId();
        Remark remark = new Remark();
        remark.setContent(content);
        remark.setBlogId(bId);
        remark.setUserId(userId);

        RemarkDao remarkDao = new RemarkDao();
        remarkDao.insert(remark);
        resp.setStatus(200);

        //输出日志
        Log log = new Log();
        log.setUserId(userId);
        log.setAction("该用户发表评论成功！");
        LogDao logDao = new LogDao();
        logDao.insert(log);
    }
}
